package logic

import (
	"context"
	"fmt"
	"shop/model/mysql"

	"shop/shoprpc/internal/svc"
	"shop/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopDelLogic {
	return &ShopDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopDelLogic) ShopDel(in *shoprpc.ShopDelRequest) (*shoprpc.ShopDelResponse, error) {
	// todo: add your logic here and delete this line

	var finduser mysql.Products
	mysql.DB.Where("userid = ?", in.Userid).Find(&finduser)
	if finduser.Userid == 0 {
		return nil, fmt.Errorf("该账号不存在，请前去登录账号")
	}
	var findusers mysql.Products
	mysql.DB.Where("id = ?", in.Id).Find(&findusers)
	if findusers.ID == 0 {
		return nil, fmt.Errorf("该商品不存在")
	}
	err := mysql.DB.Delete(&findusers).Error
	if err != nil {
		return nil, fmt.Errorf("商品删除失败")
	}

	return &shoprpc.ShopDelResponse{}, nil
}
